package com.wf.javase;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author mpk
 * @since 2024/6/22 15:35:06
 */

public class JDKProxyTest {

    public static void main(String[] args) {

        loginService loginService = (loginService) JDKProxy.getLoginProxy();

        loginService.login();
        System.out.println(89>>1);
    }
}

class JDKProxy {

    public static Object getLoginProxy() {

        loginServiceImpl loginService = new loginServiceImpl();
        return Proxy.newProxyInstance(
                ClassLoader.getSystemClassLoader(),
                new Class[]{loginService.class},
                new MyInvocationHandler(loginService)
        );
    }

}

class MyInvocationHandler implements InvocationHandler {
    private loginServiceImpl loginService;


    public MyInvocationHandler(loginServiceImpl loginService) {

        this.loginService = loginService;

    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {


        System.out.println("增加前置增强！！！！！");
        method.invoke(loginService, args);
        System.out.println("增加后置增强！！！！！");
        return loginService;
    }
}

class loginServiceImpl implements loginService {

    public void login() {
        System.out.println("login");
    }
}

interface loginService {

    public void login();
}